package com.kuanter.kuanterauto.activity;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.entity.OtherAccount;

/** 
 * @author utopia
 * @version 2013-11-4 下午3:02:11
 * 
 */
public class AccountBalanceActivity extends BaseActivity_NoBar {
	TextView account_yu_e;
	ListView otherCardList;
	List<OtherAccount> listAccount;
	OtherAccountAdapter oAdapter;
	String yue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_ye);
		Intent intent = getIntent();
		listAccount = (List<OtherAccount>) intent.getSerializableExtra("accountInfoNewList");
		yue = intent.getStringExtra("yue");
		initUI();
	}

	private void initUI() {
	    account_yu_e = (TextView) findViewById(R.id.account_yu_e);
	    otherCardList = (ListView) findViewById(R.id.otherCardList);
	    oAdapter = new OtherAccountAdapter();
	    otherCardList.setAdapter(oAdapter);
	    account_yu_e.setText(yue.replace("积分", ""));
	}
	
	public void back(View v){
		finish();
	}

	class OtherAccountAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return listAccount == null ? 0 : listAccount.size();
		}

		@Override
		public Object getItem(int i) {
			return listAccount.get(i);
		}

		@Override
		public long getItemId(int i) {
			return i;
		}

		@Override
		public View getView(int i, View view, ViewGroup viewgroup) {
			if(view == null) {
				view = View.inflate(AccountBalanceActivity.this, R.layout.account_ye_item, null);
			}
			LinearLayout lin_other = (LinearLayout) view.findViewById(R.id.lin_other);
			lin_other.removeAllViews();
			for(int j = 0; j < listAccount.get(i).getMessage().size(); j++) {
				View view_item = View.inflate(AccountBalanceActivity.this, R.layout.account_ye_other, null);
				RelativeLayout re_account_jf = (RelativeLayout) view_item.findViewById(R.id.re_account_jf);
				TextView account_number = (TextView) view_item.findViewById(R.id.account_number);
				TextView tv_jf = (TextView) view_item.findViewById(R.id.tv_jf);
				
				if(listAccount.get(i).getMessage().size() == 1) {
					re_account_jf.setBackgroundResource(R.drawable.all_corner);
				} else {
					if(j == 0) {
						re_account_jf.setBackgroundResource(R.drawable.top_corner);
						account_number.setVisibility(View.VISIBLE);
					} else if(j == listAccount.get(i).getMessage().size() - 1) {
						re_account_jf.setBackgroundResource(R.drawable.bottom_corner);
						account_number.setVisibility(View.INVISIBLE);
					} else {
						re_account_jf.setBackgroundResource(R.drawable.no_corner);
						account_number.setVisibility(View.INVISIBLE);
					}
				}
				account_number.setText(listAccount.get(i).getCardNumber());
				String balance = listAccount.get(i).getMessage().get(j).getBalance();
				String money = "0";
				try {
					double d = Double.valueOf(balance);
					money = new DecimalFormat("#0").format(d);
				} catch (Exception e) {
				}
				tv_jf.setText(listAccount.get(i).getMessage().get(j).getCategoryName()  + " " + money + " " + listAccount.get(i).getMessage().get(j).getAccountType());
				lin_other.addView(view_item);
			}
			return view;
		}
		
	}
}
